package com.flutter.mwm;

import android.content.Intent;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.google.zxing.client.android.Intents;
import com.loyal.kit.DeviceUtil;
import com.loyal.qr.ui.PortActivity;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class MainActivity extends FlutterActivity implements MethodChannel.MethodCallHandler {
    private static final String CHANNEL = "com.flutter.mwm";
    private MethodChannel.Result result;
    private static final int QR = 50;
    private static final int CAMERA = 51;

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        new MethodChannel(flutterEngine.getDartExecutor(), CHANNEL).setMethodCallHandler(this);
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        String callMethod = call.method;
        this.result = result;
        if (TextUtils.equals("deviceSerial", callMethod)) {
            result.success(DeviceUtil.deviceSerial());
        } else if (TextUtils.equals("version", callMethod)) {
            result.success(DeviceUtil.apkVersion(MainActivity.this));
        } else if (TextUtils.equals("qrCode", callMethod)) {
            Intent intent = new Intent(this, PortActivity.class);
            startActivityForResult(intent, QR);
        } else if (TextUtils.equals("camera", callMethod)) {
            /*Intent intent = new Intent(this, CameraActivity.class);
            String photoJson = call.argument(ExtraImpl.PHOTO_JSON);
            intent.putExtra(ExtraImpl.PHOTO_JSON, photoJson);
            String lsh = call.argument(ExtraImpl.LSH);
            intent.putExtra(ExtraImpl.LSH, lsh);
            String savePath = call.argument(ExtraImpl.SAVE_PATH);
            intent.putExtra(ExtraImpl.SAVE_PATH, savePath);
            startActivityForResult(intent, CAMERA);*/
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case QR:
                if (resultCode == RESULT_OK) {
                    String qrCode = null == data ? "" : data.getStringExtra(Intents.Scan.RESULT);
                    result.success(qrCode);
                } else result.success("");
                break;
            case CAMERA:
                /*if (resultCode == RESULT_OK) {
                    String cameraResult = null == data ? "" : data.getStringExtra("cameraResult");
                    result.success(cameraResult);
                } else result.success("");*/
                break;
        }
    }
}
